×
☰ See All Chapters

MySQL LPAD and RPAD Function

MySQL LPAD function is used to left-pad (add padding on the left side) a string with another string, to a specified length. MySQL RPAD function is used to right-pad (add padding on the right side) a string with another string, to a specified length.

MySQL LPAD and RPAD Function Syntax

LPAD(CHARACTERSET, VALUE1, VALUE2)

RPAD(CHARACTERSET, VALUE1, VALUE2)

There are normally three arguments with the LPAD and RPAD function:

CHARACTER SET: String on which padding is to be done

Value1: total number of characters after padding

Value2: string to be padded

MySQL LPAD and RPAD Function Example

Creating table for demonstrating LPAD and RPAD Function

CREATE TABLE NAME

(

  FNAME  VARCHAR(10 )    NOT NULL,

  LNAME  VARCHAR(10 )

);

Insert into NAME (FNAME, LNAME) Values ('ADI', 'TEMP');

Insert into NAME (FNAME, LNAME) Values ('NAVEEN', 'SHETTY');

Insert into NAME (FNAME, LNAME) Values ('ARJUN', 'SHETTY');

Insert into NAME (FNAME, LNAME) Values ('HARISH', 'GOWDA');

Insert into NAME (FNAME, LNAME) Values ('HARI', 'PRASAD');

Insert into NAME (FNAME, LNAME) Values ('ARJUN', 'SHETTY');

Insert into NAME (FNAME, LNAME) Values ('KIRAN', 'KUMAR');

COMMIT;

Example 1

SELECT FNAME,LNAME,LPAD(FNAME,10,'A'),RPAD(LNAME,20,'ABC')

FROM NAME

mysql-lpad-function-and-rpad-function-0

Example 2

SELECT FNAME,LNAME,LPAD('AAAAA',10,'A'),RPAD('BBB',20,'ABC')

FROM NAME

mysql-lpad-function-and-rpad-function-1


All Chapters
Author